home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Text / tex / rtf2LaTeX 1.5 / rtf2LaTeX / reader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-06  |  37.4 KB  |  1,538 lines  |  [TEXT/KAHL]

  1. /*
  2.     Need to document error code meanings.
  3.     Change document to reflect RTFFuncPtr.
  4.  
  5.     reader.c - RTF file reader.  Distribution 1.06a3.
  6.  
  7.     ASCII 10 (\n) and 13 (\r) are ignored and silently discarded
  8.     (although the read hook will still get a look at them.)
  9.  
  10.     "\:" is not a ":", it's a control symbol.  But some versions of
  11.     Word seem to write "\:" for ":".  This reader treats "\:" as a
  12.     plain text ":"
  13.  
  14.     This reader needs to catch \cf and \cb in the color table
  15.     reader?  (Doesn't yet.)
  16.  
  17.     19 Mar 93 Add hack to skip "{\*\keycode ... }" group in stylesheet
  18. */
  19.  
  20. # include    <stdio.h>
  21. # include    <ctype.h>
  22. # include    <string.h>
  23. # ifdef THINK_C
  24. #    include    "macintosh.h"
  25. # endif /* THINK_C */
  26. # ifdef __STDC__
  27. #    include    <stdlib.h>
  28. /* if it is a standard compiler, you should have stdarg. */
  29. #   ifndef VARARGS
  30. #       define STDARG
  31. #   endif /* !VARARGS */
  32. # else /* !__STDC__ */
  33. #    ifdef NO_MALLOC_H
  34.         extern char *malloc ();
  35. #    else /* NO_MALLOC_H */
  36. #       include    <malloc.h>
  37. #    endif /* NO_MALLOC_H */
  38. # endif /* !__STDC__ */
  39. # ifdef STDARG
  40. #    include    <stdarg.h>
  41. # else    /* !STDARG */
  42. #    ifdef VARARGS
  43. #       include    <varargs.h>
  44. #    endif /* VARARGS */
  45. # endif    /* !STDARG */
  46.  
  47. # include    "rtf.h"
  48.  
  49.  
  50. /*
  51.     Return pointer to new element of type t, or NULL
  52. */
  53.  
  54. # define    New(t)    ((t *) RTFAlloc ((int) sizeof (t)))
  55.  
  56.  
  57. # ifdef    SYSV
  58. # define    index    strchr
  59. # endif
  60.  
  61. /* The templates are needed, at least for the mac */
  62. # ifdef __STDC__
  63. static void     _RTFGetToken (void);
  64. static int      GetChar (void);
  65. static int      HexVal (char);
  66. static void     ReadFontTbl (void);
  67. static void     ReadColorTbl (void);
  68. static void     ReadStyleSheet (void);
  69. static void     ReadInfoGroup (void);
  70. static void     ReadPictGroup (void);
  71. static void     LookupInit (void);
  72. static void     Lookup (char*);
  73. static int      Hash (char*);
  74. static void     Error (char *format, ...);
  75. # else /* !__STDC__ */
  76. static void    _RTFGetToken ();
  77. static int    GetChar ();
  78. static int    HexVal ();
  79. static void    ReadFontTbl ();
  80. static void    ReadColorTbl ();
  81. static void    ReadStyleSheet ();
  82. static void    ReadInfoGroup ();
  83. static void    ReadPictGroup ();
  84. static void    LookupInit ();
  85. static void    Lookup ();
  86. static int    Hash ();
  87. static void    Error ();
  88. # endif /* !__STDC__ */
  89.  
  90.  
  91. /*
  92.     Public variables (listed in rtf.h)
  93. */
  94.  
  95. int    rtfClass;
  96. int    rtfMajor;
  97. int    rtfMinor;
  98. int    rtfParam;
  99. char    rtfTextBuf[rtfBufSiz];
  100. int    rtfTextLen;
  101.  
  102.  
  103. /*
  104.     Private stuff
  105. */
  106.  
  107. static int    pushedChar;    /* pushback char if read too far */
  108.  
  109. static int    pushedClass;    /* pushed token info for RTFUngetToken() */
  110. static int    pushedMajor;
  111. static int    pushedMinor;
  112. static int    pushedParam;
  113. static char    pushedTextBuf[rtfBufSiz];
  114.  
  115.  
  116. static RTFFont    *fontList = (RTFFont *) NULL;    /* these lists MUST be */
  117. static RTFColor    *colorList = (RTFColor *) NULL;    /* initialized to NULL */
  118. static RTFStyle    *styleList = (RTFStyle *) NULL;
  119.  
  120.  
  121. static FILE    *rtffp = stdin;
  122.  
  123.  
  124. /*
  125.     Initialize the reader.  This may be called multiple times,
  126.     to read multiple files.  The only thing not reset is the input
  127.     stream; that must be done with RTFSetStream().
  128. */
  129.  
  130. void RTFInit ()
  131. {
  132. int    i;
  133. RTFColor    *cp;
  134. RTFFont        *fp;
  135. RTFStyle    *sp;
  136. RTFStyleElt    *eltList, *ep;
  137.  
  138.     /* initialize lookup table */
  139.     LookupInit ();
  140.  
  141.     for (i = 0; i < rtfMaxClass; i++)
  142.         RTFSetClassCallback (i, (RTFFuncPtr) NULL);
  143.     for (i = 0; i < rtfMaxDestination; i++)
  144.         RTFSetDestinationCallback (i, (RTFFuncPtr) NULL);
  145.  
  146.     /* install built-in destination readers */
  147.     RTFSetDestinationCallback (rtfFontTbl, ReadFontTbl);
  148.     RTFSetDestinationCallback (rtfColorTbl, ReadColorTbl);
  149.     RTFSetDestinationCallback (rtfStyleSheet, ReadStyleSheet);
  150.     RTFSetDestinationCallback (rtfInfo, ReadInfoGroup);
  151.     RTFSetDestinationCallback (rtfPict, ReadPictGroup);
  152.  
  153.     RTFSetReadHook ((RTFFuncPtr) NULL);
  154.  
  155.     /* dump old lists if necessary */
  156.  
  157.     while (fontList != (RTFFont *) NULL)
  158.     {
  159.         fp = fontList->rtfNextFont;
  160.         RTFFree (fontList->rtfFName);
  161.         RTFFree ((char *) fontList);
  162.         fontList = fp;
  163.     }
  164.     while (colorList != (RTFColor *) NULL)
  165.     {
  166.         cp = colorList->rtfNextColor;
  167.         RTFFree ((char *) colorList);
  168.         colorList = cp;
  169.     }
  170.     while (styleList != (RTFStyle *) NULL)
  171.     {
  172.         sp = styleList->rtfNextStyle;
  173.         eltList = styleList->rtfSSEList;
  174.         while (eltList != (RTFStyleElt *) NULL)
  175.         {
  176.             ep = eltList->rtfNextSE;
  177.             RTFFree (eltList->rtfSEText);
  178.             RTFFree ((char *) eltList);
  179.             eltList = ep;
  180.         }
  181.         RTFFree (styleList->rtfSName);
  182.         RTFFree ((char *) styleList);
  183.         styleList = sp;
  184.     }
  185.  
  186.     rtfClass = -1;
  187.     pushedClass = -1;
  188.     pushedChar = EOF;
  189. }
  190.  
  191.  
  192. /*
  193.     Set the reader's input stream to the given stream.  Can
  194.     be used to redirect to other than the default (stdin).
  195. */
  196.  
  197. void RTFSetStream (stream)
  198. FILE    *stream;
  199. {
  200.     rtffp = stream;
  201. }
  202.  
  203.  
  204. /* ---------------------------------------------------------------------- */
  205.  
  206. /*
  207.     Callback table manipulation routines
  208. */
  209.  
  210.  
  211. /*
  212.     Install or return a writer callback for a token class
  213. */
  214.  
  215.  
  216. static RTFFuncPtr    ccb[rtfMaxClass];        /* class callbacks */
  217.  
  218. void RTFSetClassCallback (class, callback)
  219. int        class;
  220. RTFFuncPtr    callback;
  221. {
  222.     if (class >= 0 && class < rtfMaxClass)
  223.         ccb[class] = callback;
  224. }
  225.  
  226.  
  227. RTFFuncPtr RTFGetClassCallback (class)
  228. int    class;
  229. {
  230.     if (class >= 0 && class < rtfMaxClass)
  231.         return (ccb[class]);
  232.     return ((RTFFuncPtr) NULL);
  233. }
  234.  
  235.  
  236. /*
  237.     Install or return a writer callback for a destination type
  238. */
  239.  
  240. static RTFFuncPtr    dcb[rtfMaxDestination];    /* destination callbacks */
  241.  
  242. void RTFSetDestinationCallback (dest, callback)
  243. int        dest;
  244. RTFFuncPtr    callback;
  245. {
  246.     if (dest >= 0 && dest < rtfMaxDestination)
  247.         dcb[dest] = callback;
  248. }
  249.  
  250.  
  251. RTFFuncPtr RTFGetDestinationCallback (dest)
  252. int    dest;
  253. {
  254.     if (dest >= 0 && dest < rtfMaxDestination)
  255.         return (dcb[dest]);
  256.     return ((RTFFuncPtr) NULL);
  257. }
  258.  
  259.  
  260. /* ---------------------------------------------------------------------- */
  261.  
  262. /*
  263.     Token reading routines
  264. */
  265.  
  266.  
  267. /*
  268.     Read the input stream, invoking the writer's callbacks
  269.     where appropriate.
  270. */
  271.  
  272. void RTFRead ()
  273. {
  274.     while (RTFGetToken () != rtfEOF)
  275.         RTFRouteToken ();
  276. }
  277.  
  278.  
  279. /*
  280.     Route a token.  If it's a destination for which a reader is
  281.     installed, process the destination internally, otherwise
  282.     pass the token to the writer's class callback.
  283. */
  284.  
  285. void RTFRouteToken ()
  286. {
  287. RTFFuncPtr    p;
  288.  
  289.     if (rtfClass < 0 || rtfClass >= rtfMaxClass)    /* watchdog */
  290.     {
  291.         Error ("Unknown class %d: %s (reader malfunction)",
  292.                             rtfClass, rtfTextBuf);
  293.     }
  294.     if (RTFCheckCM (rtfControl, rtfDestination))
  295.     {
  296.         /* invoke destination-specific callback if there is one */
  297.         if ((p = RTFGetDestinationCallback (rtfMinor))
  298.                             != (RTFFuncPtr) NULL)
  299.         {
  300.             (*p) ();
  301.             return;
  302.         }
  303.     }
  304.     /* invoke class callback if there is one */
  305.     if ((p = RTFGetClassCallback (rtfClass)) != (RTFFuncPtr) NULL)
  306.         (*p) ();
  307. }
  308.  
  309.  
  310. /*
  311.     Skip to the end of the current group.  When this returns,
  312.     writers that maintain a state stack may want to call their
  313.     state unstacker; global vars will still be set to the group's
  314.     closing brace.
  315. */
  316.  
  317. void RTFSkipGroup ()
  318. {
  319. int    level = 1;
  320.  
  321.     while (RTFGetToken () != rtfEOF)
  322.     {
  323.         if (rtfClass == rtfGroup)
  324.         {
  325.             if (rtfMajor == rtfBeginGroup)
  326.                 ++level;
  327.             else if (rtfMajor == rtfEndGroup)
  328.             {
  329.                 if (--level < 1)
  330.                     break;    /* end of initial group */
  331.             }
  332.         }
  333.     }
  334. }
  335.  
  336.  
  337. /*
  338.     Read one token.  Call the read hook if there is one.  The
  339.     token class is the return value.  Returns rtfEOF when there
  340.     are no more tokens.
  341. */
  342.  
  343. int RTFGetToken ()
  344. {
  345. RTFFuncPtr    p;
  346.  
  347.     for (;;)
  348.     {
  349.         _RTFGetToken ();
  350.         if ((p = RTFGetReadHook ()) != (RTFFuncPtr) NULL)
  351.             (*p) ();    /* give read hook a look at token */
  352.  
  353.         /* Silently discard newlines, carriage returns, nulls.  */
  354.         if (!(rtfClass == rtfText
  355.             && (rtfMajor == '\n' || rtfMajor == '\r'
  356.                         || rtfMajor == '\0')))
  357.             break;
  358.     }
  359.     return (rtfClass);
  360. }
  361.  
  362.  
  363. /*
  364.     Install or return a token reader hook.
  365. */
  366.  
  367. static RTFFuncPtr    readHook;
  368.  
  369. void RTFSetReadHook (f)
  370. RTFFuncPtr    f;
  371. {
  372.     readHook = f;
  373. }
  374.  
  375.  
  376. RTFFuncPtr RTFGetReadHook ()
  377. {
  378.     return (readHook);
  379. }
  380.  
  381.  
  382. void RTFUngetToken ()
  383. {
  384.     if (pushedClass >= 0)    /* there's already an ungotten token */
  385.         Error ("cannot unget two tokens");
  386.     if (rtfClass < 0)
  387.         Error ("no token to unget");
  388.     pushedClass = rtfClass;
  389.     pushedMajor = rtfMajor;
  390.     pushedMinor = rtfMinor;
  391.     pushedParam = rtfParam;
  392.     (void) strcpy (pushedTextBuf, rtfTextBuf);
  393. }
  394.  
  395.  
  396. int RTFPeekToken ()
  397. {
  398.     _RTFGetToken ();
  399.     RTFUngetToken ();
  400.     return (rtfClass);
  401. }
  402.  
  403.  
  404. static void _RTFGetToken ()
  405. {
  406. int    sign;
  407. int    c;
  408.  
  409.     /* check for pushed token from RTFUngetToken() */
  410.  
  411.     if (pushedClass >= 0)
  412.     {
  413.         rtfClass = pushedClass;
  414.         rtfMajor = pushedMajor;
  415.         rtfMinor = pushedMinor;
  416.         rtfParam = pushedParam;
  417.         (void) strcpy (rtfTextBuf, pushedTextBuf);
  418.         rtfTextLen = strlen (rtfTextBuf);
  419.         pushedClass = -1;
  420.         return;
  421.     }
  422.  
  423.     /* initialize token vars */
  424.  
  425.     rtfClass = rtfUnknown;
  426.     rtfParam = rtfNoParam;
  427.     rtfTextBuf[rtfTextLen = 0] = '\0';
  428.  
  429.     /* get first character, which may be a pushback from previous token */
  430.  
  431.     if (pushedChar != EOF)
  432.     {
  433.         c = pushedChar;
  434.         rtfTextBuf[rtfTextLen] = c;
  435.         rtfTextBuf[++rtfTextLen] = '\0';
  436.         pushedChar = EOF;
  437.     }
  438.     else if ((c = GetChar ()) == EOF)
  439.     {
  440.         rtfClass = rtfEOF;
  441.         return;
  442.     }
  443.  
  444.     if (c == '{')
  445.     {
  446.         rtfClass = rtfGroup;
  447.         rtfMajor = rtfBeginGroup;
  448.         return;
  449.     }
  450.     if (c == '}')
  451.     {
  452.         rtfClass = rtfGroup;
  453.         rtfMajor = rtfEndGroup;
  454.         return;
  455.     }
  456.     if (c != '\\')
  457.     {
  458.         /*
  459.             Two possibilities here:
  460.             1) ASCII 9, effectively like \tab control symbol
  461.             2) literal text char
  462.         */
  463.         if (c == '\t')            /* ASCII 9 */
  464.         {
  465.             rtfClass = rtfControl;
  466.             rtfMajor = rtfSpecialChar;
  467.             rtfMinor = rtfTab;
  468.         }
  469.         else
  470.         {
  471.             rtfClass = rtfText;
  472.             rtfMajor = c;
  473.         }
  474.         return;
  475.     }
  476.     if ((c = GetChar ()) == EOF)
  477.     {
  478.         /* early eof, whoops (class is rtfUnknown) */
  479.         return;
  480.     }
  481.     if (!isalpha (c))
  482.     {
  483.         /*
  484.             Three possibilities here:
  485.             1) hex encoded text char, e.g., \'d5, \'d3
  486.             2) special escaped text char, e.g., \{, \}
  487.             3) control symbol, e.g., \_, \-, \|, \<10>
  488.         */
  489.         if (c == '\'')                /* hex char */
  490.         {
  491.         int    c2;
  492.  
  493.             if ((c = GetChar ()) != EOF && (c2 = GetChar ()) != EOF)
  494.             {
  495.                 /* should do isxdigit check! */
  496.                 rtfClass = rtfText;
  497.                 rtfMajor = HexVal (c) * 16 + HexVal (c2);
  498.                 return;
  499.             }
  500.             /* early eof, whoops (class is rtfUnknown) */
  501.             return;
  502.         }
  503.  
  504.         if (index (":{}\\", c) != (char *) NULL) /* escaped char */
  505.         {
  506.             rtfClass = rtfText;
  507.             rtfMajor = c;
  508.             return;
  509.         }
  510.  
  511.         /* control symbol */
  512.         Lookup (rtfTextBuf);    /* sets class, major, minor */
  513.         return;
  514.     }
  515.     /* control word */
  516.     while (isalpha (c))
  517.     {
  518.         if ((c = GetChar ()) == EOF)
  519.             break;
  520.     }
  521.  
  522.     /*
  523.         At this point, the control word is all collected, so the
  524.         major/minor numbers are determined before the parameter
  525.         (if any) is scanned.  There will be one too many characters
  526.         in the buffer, though, so fix up before and restore after
  527.         looking up.
  528.     */
  529.  
  530.     if (c != EOF)
  531.         rtfTextBuf[rtfTextLen-1] = '\0';
  532.     Lookup (rtfTextBuf);    /* sets class, major, minor */
  533.     if (c != EOF)
  534.         rtfTextBuf[rtfTextLen-1] = c;
  535.  
  536.     /*
  537.         Should be looking at first digit of parameter if there
  538.         is one, unless it's negative.  In that case, next char
  539.         is '-', so need to gobble next char, and remember sign.
  540.     */
  541.  
  542.     sign = 1;
  543.     if (c == '-')
  544.     {
  545.         sign = -1;
  546.         c = GetChar ();
  547.     }
  548.     if (c != EOF && isdigit (c))
  549.     {
  550.         rtfParam = 0;
  551.         while (isdigit (c))    /* gobble parameter */
  552.         {
  553.             rtfParam = rtfParam * 10 + c - '0';
  554.             if ((c = GetChar ()) == EOF)
  555.                 break;
  556.         }
  557.         rtfParam *= sign;
  558.     }
  559.     /*
  560.         If control symbol delimiter was a blank, gobble it.
  561.         Otherwise the character is first char of next token, so
  562.         push it back for next call.  In either case, delete the
  563.         delimiter from the token buffer.
  564.     */
  565.     if (c != EOF)
  566.     {
  567.         if (c != ' ')
  568.             pushedChar = c;
  569.         rtfTextBuf[--rtfTextLen] = '\0';
  570.     }
  571.     return;
  572. }
  573.  
  574.  
  575. /*
  576.     Distributions up through 1.04 assumed high bit could be set in
  577.     RTF file characters.  Beginning with 1.05, that's not true, but
  578.     still check and ignore such characters.  (Cope with things like
  579.     WriteNow on NeXT, which generates bad RTF by writing 8-bit
  580.     characters.)
  581. */
  582.  
  583. static int GetChar ()
  584. {
  585. int    c;
  586.  
  587.     if ((c = getc (rtffp)) != EOF)
  588.     {
  589.         if (c & 0x80)
  590.         {
  591.             fprintf (stderr, "Character found with high bit set");
  592.             fprintf (stderr, " (%#x) -> changed to '?'\n", c);
  593.             c = '?';
  594.         }
  595.         rtfTextBuf[rtfTextLen] = c;
  596.         rtfTextBuf[++rtfTextLen] = '\0';
  597.     }
  598.     return (c);
  599. }
  600.  
  601.  
  602. static int HexVal (c)
  603. char    c;
  604. {
  605.     if (isupper (c))
  606.         c = tolower (c);
  607.     if (isdigit (c))
  608.         return (c - '0');    /* '0'..'9' */
  609.     return (c - 'a' + 10);        /* 'a'..'f' */
  610. }
  611.  
  612.  
  613. /*
  614.     Synthesize a token by setting the global variables to the
  615.     values supplied.  Typically this is followed with a call
  616.     to RTFRouteToken().
  617.  
  618.     If param is non-negative, it becomes part of the token text.
  619. */
  620.  
  621. void RTFSetToken (class, major, minor, param, text)
  622. int    class, major, minor, param;
  623. char    *text;
  624. {
  625.     rtfClass = class;
  626.     rtfMajor = major;
  627.     rtfMinor = minor;
  628.     rtfParam = param;
  629.     if (param == rtfNoParam)
  630.         (void) strcpy (rtfTextBuf, text);
  631.     else
  632.         sprintf (rtfTextBuf, "%s%d", text, param);
  633.     rtfTextLen = strlen (rtfTextBuf);
  634. }
  635.  
  636.  
  637. /* ---------------------------------------------------------------------- */
  638.  
  639. /*
  640.     Special destination readers.  They gobble the destination so the
  641.     writer doesn't have to deal with them.  That's wrong for any
  642.     translator that wants to process any of these itself.  In that
  643.     case, these readers should be overridden by installing a different
  644.     destination callback.
  645.  
  646.     NOTE: The last token read by each of these reader will be the
  647.     destination's terminating '}', which will then be the current token.
  648.     That '}' token is passed to RTFRouteToken() - the writer has already
  649.     seen the '{' that began the destination group, and may have pushed a
  650.     state; it also needs to know at the end of the group that a state
  651.     should be popped.
  652.  
  653.     It's important that rtf.h and the control token lookup table list
  654.     as many symbols as possible, because these readers unfortunately
  655.     make strict assumptions about the input they expect, and a token
  656.     of class rtfUnknown will throw them off easily.
  657. */
  658.  
  659.  
  660. /*
  661.     Read { \fonttbl ... } destination.  Old font tables don't have
  662.     braces around each table entry; try to adjust for that.
  663. */
  664.  
  665. static void ReadFontTbl ()
  666. {
  667. RTFFont    *fp;
  668. char    buf[rtfBufSiz], *bp;
  669. int    old = -1;
  670.  
  671.     for (;;)
  672.     {
  673.         (void) RTFGetToken ();
  674.         if (RTFCheckCM (rtfGroup, rtfEndGroup))
  675.             break;
  676.         if (old < 0)        /* first entry - determine tbl type */
  677.         {
  678.             if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
  679.                 old = 1;    /* no brace */
  680.             else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
  681.                 old = 0;    /* brace */
  682.             else            /* can't tell! */
  683.                 Error ("FTErr - Cannot determine format");
  684.         }
  685.         if (old == 0)        /* need to find "{" here */
  686.         {
  687.             if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
  688.                 Error ("FTErr - missing \"{\"");
  689.             (void) RTFGetToken ();    /* yes, skip to next token */
  690.         }
  691.         if ((fp = New (RTFFont)) == (RTFFont *) NULL)
  692.             Error ("FTErr - cannot allocate font entry");
  693.         fp->rtfNextFont = fontList;
  694.         fontList = fp;
  695.         if (!RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
  696.             Error ("FTErr - missing font number");
  697.         fp->rtfFNum = rtfParam;
  698.         (void) RTFGetToken ();
  699.         if (!RTFCheckCM (rtfControl, rtfFontFamily))
  700.             Error ("FTErr - missing font family");
  701.         fp->rtfFFamily = rtfMinor;
  702.         bp = buf;
  703.         while (RTFGetToken () == rtfText)
  704.         {
  705.             if (rtfMajor == ';')
  706.                 break;
  707.             *bp++ = rtfMajor;
  708.         }
  709.         *bp = '\0';
  710.         if (buf[0] == '\0')
  711.             Error ("FTErr - missing font name");
  712.         if ((fp->rtfFName = RTFStrSave (buf)) == (char *) NULL)
  713.             Error ("FTErr - cannot allocate font name");
  714.         if (old == 0)    /* need to see "}" here */
  715.         {
  716.             (void) RTFGetToken ();
  717.             if (!RTFCheckCM (rtfGroup, rtfEndGroup))
  718.                 Error ("FTErr - missing \"}\"");
  719.         }
  720.     }
  721.     RTFRouteToken ();    /* feed "}" back to router */
  722. }
  723.  
  724.  
  725. /*
  726.     The color table entries have color values of -1 if
  727.     the default color should be used for the entry (only
  728.     a semi-colon is given in the definition, no color values).
  729.     There will be a problem if a partial entry (1 or 2 but
  730.     not 3 color values) is given.  The possibility is ignored
  731.     here.
  732. */
  733.  
  734. static void ReadColorTbl ()
  735. {
  736. RTFColor    *cp;
  737. int        cnum = 0;
  738.  
  739.     for (;;)
  740.     {
  741.         (void) RTFGetToken ();
  742.         if (RTFCheckCM (rtfGroup, rtfEndGroup))
  743.             break;
  744.         if ((cp = New (RTFColor)) == (RTFColor *) NULL)
  745.             Error ("CTErr - cannot allocate color entry");
  746.         cp->rtfCNum = cnum++;
  747.         cp->rtfCRed = cp->rtfCGreen = cp->rtfCBlue = -1;
  748.         cp->rtfNextColor = colorList;
  749.         colorList = cp;
  750.         for (;;)
  751.         {
  752.             if (!RTFCheckCM (rtfControl, rtfColorName))
  753.                 break;
  754.             switch (rtfMinor)
  755.             {
  756.             case rtfRed:    cp->rtfCRed = rtfParam; break;
  757.             case rtfGreen:    cp->rtfCGreen = rtfParam; break;
  758.             case rtfBlue:    cp->rtfCBlue = rtfParam; break;
  759.             }
  760.             RTFGetToken ();
  761.         }
  762.         if (!RTFCheckCM (rtfText, (int) ';'))
  763.             Error ("CTErr - malformed entry");
  764.     }
  765.     RTFRouteToken ();    /* feed "}" back to router */
  766. }
  767.  
  768.  
  769. /*
  770.     The "Normal" style definition doesn't contain any style number
  771.     (why?), all others do.  Normal style is given style 0.
  772. */
  773.  
  774. static void ReadStyleSheet ()
  775. {
  776. RTFStyle    *sp;
  777. RTFStyleElt    *sep, *sepLast;
  778. char        buf[rtfBufSiz], *bp;
  779.  
  780.     for (;;)
  781.     {
  782.         (void) RTFGetToken ();
  783.         if (RTFCheckCM (rtfGroup, rtfEndGroup))
  784.             break;
  785.         if ((sp = New (RTFStyle)) == (RTFStyle *) NULL)
  786.             Error ("SSErr - cannot allocate stylesheet entry");
  787.         sp->rtfSNum = -1;
  788.         sp->rtfSBasedOn = rtfBasedOnNone;
  789.         sp->rtfSNextPar = -1;
  790.         sp->rtfSSEList = sepLast = (RTFStyleElt *) NULL;
  791.         sp->rtfNextStyle = styleList;
  792.         sp->rtfExpanding = 0;
  793.         styleList = sp;
  794.         if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
  795.             Error ("SSErr - missing \"{\"");
  796.         for (;;)
  797.         {
  798.             /*
  799.                 This passes over "{\*\keycode ... }".  A
  800.                 temporary (hopefully) hack.
  801.             */
  802.             if (RTFGetToken () != rtfControl)
  803.             {
  804.                 if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
  805.                     break;
  806.                 RTFSkipGroup ();
  807.                 continue;
  808.             }
  809.             if (RTFCheckMM (rtfParAttr, rtfStyleNum))
  810.             {
  811.                 sp->rtfSNum = rtfParam;
  812.                 continue;
  813.             }
  814.             if (RTFCheckMM (rtfStyleAttr, rtfBasedOn))
  815.             {
  816.                 sp->rtfSBasedOn = rtfParam;
  817.                 continue;
  818.             }
  819.             if (RTFCheckMM (rtfStyleAttr, rtfNext))
  820.             {
  821.                 sp->rtfSNextPar = rtfParam;
  822.                 continue;
  823.             }
  824.             if ((sep = New (RTFStyleElt)) == (RTFStyleElt *) NULL)
  825.                 Error ("SSErr - cannot allocate style element");
  826.             sep->rtfSEClass = rtfClass;
  827.             sep->rtfSEMajor = rtfMajor;
  828.             sep->rtfSEMinor = rtfMinor;
  829.             sep->rtfSEParam = rtfParam;
  830.             if ((sep->rtfSEText = RTFStrSave (rtfTextBuf))
  831.                             == (char *) NULL)
  832.                 Error ("SSErr - cannot allocate style element text");
  833.             if (sepLast == (RTFStyleElt *) NULL)
  834.                 sp->rtfSSEList = sep;    /* first element */
  835.             else                /* add to end */
  836.                 sepLast->rtfNextSE = sep;
  837.             sep->rtfNextSE = (RTFStyleElt *) NULL;
  838.             sepLast = sep;
  839.         }
  840.         if (sp->rtfSNextPar == -1)        /* \snext not given */
  841.             sp->rtfSNextPar = sp->rtfSNum;    /* next is itself */
  842.         if (rtfClass != rtfText)
  843.             Error ("SSErr - missing style name");
  844.         bp = buf;
  845.         while (rtfClass == rtfText)
  846.         {
  847.             if (rtfMajor == ';')
  848.             {
  849.                 (void) RTFGetToken ();
  850.                 break;
  851.             }
  852.             *bp++ = rtfMajor;
  853.             (void) RTFGetToken ();
  854.         }
  855.         *bp = '\0';
  856.         /*
  857.             If no style number was specified, check whether it's
  858.             the Normal style (in which case it's style 0).  Note
  859.             that some "normal" style names just begin with
  860.             "Normal" and can have other stuff following, e.g.,
  861.             "Normal,Times 10 point".  Ugh.
  862.  
  863.             Some German writers use "Standard" instead of "Normal".
  864.         */
  865.         if (sp->rtfSNum < 0)
  866.         {
  867.             if (strncmp (buf, "Normal", 6) != 0
  868.                 && strncmp (buf, "Standard", 8) != 0)
  869.                 Error ("SSErr - missing style number");
  870.             sp->rtfSNum = 0;
  871.         }
  872.         if ((sp->rtfSName = RTFStrSave (buf)) == (char *) NULL)
  873.             Error ("SSErr - cannot allocate style name");
  874.         if (!RTFCheckCM (rtfGroup, rtfEndGroup))
  875.             Error ("SSErr - missing \"}\"");
  876.     }
  877.     RTFRouteToken ();    /* feed "}" back to router */
  878. }
  879.  
  880.  
  881. static void ReadInfoGroup ()
  882. {
  883.     RTFSkipGroup ();
  884.     RTFRouteToken ();    /* feed "}" back to router */
  885. }
  886.  
  887.  
  888. static void ReadPictGroup ()
  889. {
  890.     RTFSkipGroup ();
  891.     RTFRouteToken ();    /* feed "}" back to router */
  892. }
  893.  
  894.  
  895. /* ---------------------------------------------------------------------- */
  896.  
  897. /*
  898.     Routines to return pieces of stylesheet, or font or color tables
  899. */
  900.  
  901.  
  902. RTFStyle *RTFGetStyle (num)
  903. int    num;
  904. {
  905. RTFStyle    *s;
  906.  
  907.     if (num == -1)
  908.         return (styleList);
  909.     for (s = styleList; s != (RTFStyle *) NULL; s = s->rtfNextStyle)
  910.     {
  911.         if (s->rtfSNum == num)
  912.             break;
  913.     }
  914.     return (s);        /* NULL if not found */
  915. }
  916.  
  917.  
  918. RTFFont *RTFGetFont (num)
  919. int    num;
  920. {
  921. RTFFont    *f;
  922.  
  923.     if (num == -1)
  924.         return (fontList);
  925.     for (f = fontList; f != (RTFFont *) NULL; f = f->rtfNextFont)
  926.     {
  927.         if (f->rtfFNum == num)
  928.             break;
  929.     }
  930.     return (f);        /* NULL if not found */
  931. }
  932.  
  933.  
  934. RTFColor *RTFGetColor (num)
  935. int    num;
  936. {
  937. RTFColor    *c;
  938.  
  939.     if (num == -1)
  940.         return (colorList);
  941.     for (c = colorList; c != (RTFColor *) NULL; c = c->rtfNextColor)
  942.     {
  943.         if (c->rtfCNum == num)
  944.             break;
  945.     }
  946.     return (c);        /* NULL if not found */
  947. }
  948.  
  949.  
  950. /* ---------------------------------------------------------------------- */
  951.  
  952.  
  953. /*
  954.     Expand style n, if there is such a style.
  955. */
  956.  
  957. void RTFExpandStyle (n)
  958. int    n;
  959. {
  960. RTFStyle    *s;
  961. RTFStyleElt    *se;
  962.  
  963.     if (n == -1 || (s = RTFGetStyle (n)) == (RTFStyle *) NULL)
  964.         return;
  965.     if (s->rtfExpanding != 0)
  966.         Error ("Style expansion loop, style %d", n);
  967.     s->rtfExpanding = 1;    /* set expansion flag for loop detection */
  968.     /*
  969.         Expand "based-on" style.  This is done by synthesizing
  970.         the token that the writer needs to see in order to trigger
  971.         another style expansion, and feeding to token back through
  972.         the router so the writer sees it.
  973.     */
  974.     RTFSetToken (rtfControl, rtfParAttr, rtfStyleNum, s->rtfSBasedOn, "\\s");
  975.     RTFRouteToken ();
  976.     /*
  977.         Now route the tokens unique to this style.  RTFSetToken()
  978.         isn't used because it would add the param value to the end
  979.         of the token text, which already has it in.
  980.     */
  981.     for (se = s->rtfSSEList; se != (RTFStyleElt *) NULL; se = se->rtfNextSE)
  982.     {
  983.         rtfClass = se->rtfSEClass;
  984.         rtfMajor = se->rtfSEMajor;
  985.         rtfMinor = se->rtfSEMinor;
  986.         rtfParam = se->rtfSEParam;
  987.         (void) strcpy (rtfTextBuf, se->rtfSEText);
  988.         rtfTextLen = strlen (rtfTextBuf);
  989.         RTFRouteToken ();
  990.     }
  991.     s->rtfExpanding = 0;    /* done - clear expansion flag */
  992. }
  993.  
  994.  
  995. /* ---------------------------------------------------------------------- */
  996.  
  997. /*
  998.     Control symbol lookup routines
  999. */
  1000.  
  1001.  
  1002. typedef struct RTFKey    RTFKey;
  1003.  
  1004. struct RTFKey
  1005. {
  1006.     int    rtfKMajor;    /* major number */
  1007.     int    rtfKMinor;    /* minor number */
  1008.     char    *rtfKStr;    /* symbol name */
  1009.     int    rtfKHash;    /* symbol name hash value */
  1010. };
  1011.  
  1012. /*
  1013.     A minor number of -1 means the token has no minor number
  1014.     (all valid minor numbers are >= 0).
  1015. */
  1016.  
  1017. static RTFKey    rtfKey[] =
  1018. {
  1019.     rtfSpecialChar,    rtfCurHeadPict,        "chpict",    0,    /* ?? */
  1020.  
  1021.     rtfSpecialChar,    rtfCurHeadDate,        "chdate",    0,
  1022.     rtfSpecialChar,    rtfCurHeadTime,        "chtime",    0,
  1023.     rtfSpecialChar,    rtfCurHeadPage,        "chpgn",    0,
  1024.     rtfSpecialChar,    rtfCurFNote,        "chftn",    0,
  1025.     rtfSpecialChar,    rtfCurAnnotRef,        "chatn",    0,
  1026.     rtfSpecialChar,    rtfFNoteSep,        "chftnsep",    0,
  1027.     rtfSpecialChar,    rtfFNoteCont,        "chftnsepc",    0,
  1028.     rtfSpecialChar,    rtfFormula,        "|",        0,
  1029.     rtfSpecialChar,    rtfNoBrkSpace,        "~",        0,
  1030.     rtfSpecialChar,    rtfNoReqHyphen,        "-",        0,
  1031.     rtfSpecialChar,    rtfNoBrkHyphen,        "_",        0,
  1032.     rtfSpecialChar,    rtfCell,        "cell",        0,
  1033.     rtfSpecialChar,    rtfRow,            "row",        0,
  1034.     rtfSpecialChar,    rtfPar,            "par",        0,
  1035.     rtfSpecialChar,    rtfPar,            "\n",        0,
  1036.     rtfSpecialChar,    rtfPar,            "\r",        0,
  1037.     rtfSpecialChar,    rtfSect,        "sect",        0,
  1038.     rtfSpecialChar,    rtfPage,        "page",        0,
  1039.     rtfSpecialChar,    rtfColumn,        "column",    0,
  1040.     rtfSpecialChar,    rtfLine,        "line",        0,
  1041.     rtfSpecialChar,    rtfTab,            "tab",        0,
  1042.     rtfSpecialChar,    rtfOptDest,        "*",        0,
  1043.     rtfSpecialChar,    rtfIIntVersion,        "vern",        0,
  1044.     rtfSpecialChar,    rtfICreateTime,        "creatim",    0,
  1045.     rtfSpecialChar,    rtfIRevisionTime,    "revtim",    0,
  1046.     rtfSpecialChar,    rtfIPrintTime,        "printim",    0,
  1047.     rtfSpecialChar,    rtfIBackupTime,        "buptim",    0,
  1048.     rtfSpecialChar,    rtfIEditTime,        "edmins",    0,
  1049.     rtfSpecialChar,    rtfIYear,        "yr",        0,
  1050.     rtfSpecialChar,    rtfIMonth,        "mo",        0,
  1051.     rtfSpecialChar,    rtfIDay,        "dy",        0,
  1052.     rtfSpecialChar,    rtfIHour,        "hr",        0,
  1053.     rtfSpecialChar,    rtfIMinute,        "min",        0,
  1054.     rtfSpecialChar,    rtfINPages,        "nofpages",    0,
  1055.     rtfSpecialChar,    rtfINWords,        "nofwords",    0,
  1056.     rtfSpecialChar,    rtfINChars,        "nofchars",    0,
  1057.     rtfSpecialChar,    rtfIIntID,        "id",        0,
  1058.     rtfSpecialChar,    rtfBullet,        "bullet",    0,
  1059.     rtfSpecialChar,    rtfEmDash,        "emdash",    0,
  1060.     rtfSpecialChar,    rtfEnDash,        "endash",    0,
  1061.     rtfSpecialChar,    rtfLQuote,        "lquote",    0,
  1062.     rtfSpecialChar,    rtfRQuote,        "rquote",    0,
  1063.     rtfSpecialChar,    rtfLDblQuote,        "ldblquote",    0,
  1064.     rtfSpecialChar,    rtfRDblQuote,        "rdblquote",    0,
  1065.  
  1066.     rtfCharAttr,    rtfPlain,        "plain",    0,
  1067.     rtfCharAttr,    rtfBold,        "b",        0,
  1068.     rtfCharAttr,    rtfItalic,        "i",        0,
  1069.     rtfCharAttr,    rtfStrikeThru,        "strike",    0,
  1070.     rtfCharAttr,    rtfOutline,        "outl",        0,
  1071.     rtfCharAttr,    rtfShadow,        "shad",        0,
  1072.     rtfCharAttr,    rtfSmallCaps,        "scaps",    0,
  1073.     rtfCharAttr,    rtfAllCaps,        "caps",        0,
  1074.     rtfCharAttr,    rtfInvisible,        "v",        0,
  1075.     rtfCharAttr,    rtfFontNum,        "f",        0,
  1076.     rtfCharAttr,    rtfFontSize,        "fs",        0,
  1077.     rtfCharAttr,    rtfExpand,        "expnd",    0,
  1078.     rtfCharAttr,    rtfUnderline,        "ul",        0,
  1079.     rtfCharAttr,    rtfWUnderline,        "ulw",        0,
  1080.     rtfCharAttr,    rtfDUnderline,        "uld",        0,
  1081.     rtfCharAttr,    rtfDbUnderline,        "uldb",        0,
  1082.     rtfCharAttr,    rtfNoUnderline,        "ulnone",    0,
  1083.     rtfCharAttr,    rtfSuperScript,        "up",        0,
  1084.     rtfCharAttr,    rtfSubScript,        "dn",        0,
  1085.     rtfCharAttr,    rtfRevised,        "revised",    0,
  1086.     rtfCharAttr,    rtfForeColor,        "cf",        0,
  1087.     rtfCharAttr,    rtfBackColor,        "cb",        0,
  1088.     rtfCharAttr,    rtfGray,        "gray",        0,
  1089.     rtfCharAttr,    rtfLanguage,        "lang",        0,
  1090.  
  1091.     rtfParAttr,    rtfParDef,        "pard",        0,
  1092.     rtfParAttr,    rtfStyleNum,        "s",        0,
  1093.     /* next two are variants for \s that have been seen in Germany */
  1094.     rtfParAttr,    rtfStyleNum,        "cs",        0,
  1095.     rtfParAttr,    rtfStyleNum,        "ds",        0,
  1096.     rtfParAttr,    rtfQuadLeft,        "ql",        0,
  1097.     rtfParAttr,    rtfQuadRight,        "qr",        0,
  1098.     rtfParAttr,    rtfQuadJust,        "qj",        0,
  1099.     rtfParAttr,    rtfQuadCenter,        "qc",        0,
  1100.     rtfParAttr,    rtfFirstIndent,        "fi",        0,
  1101.     rtfParAttr,    rtfLeftIndent,        "li",        0,
  1102.     rtfParAttr,    rtfRightIndent,        "ri",        0,
  1103.     rtfParAttr,    rtfSpaceBefore,        "sb",        0,
  1104.     rtfParAttr,    rtfSpaceAfter,        "sa",        0,
  1105.     rtfParAttr,    rtfSpaceBetween,    "sl",        0,
  1106.     rtfParAttr,    rtfInTable,        "intbl",    0,
  1107.     rtfParAttr,    rtfKeep,        "keep",        0,
  1108.     rtfParAttr,    rtfKeepNext,        "keepn",    0,
  1109.     rtfParAttr,    rtfSideBySide,        "sbys",        0,
  1110.     rtfParAttr,    rtfPBBefore,        "pagebb",    0,
  1111.     rtfParAttr,    rtfNoLineNum,        "noline",    0,
  1112.     rtfParAttr,    rtfTabPos,        "tx",        0,
  1113.     rtfParAttr,    rtfTabRight,        "tqr",        0,
  1114.     rtfParAttr,    rtfTabCenter,        "tqc",        0,
  1115.     rtfParAttr,    rtfTabDecimal,        "tqdec",    0,
  1116.     rtfParAttr,    rtfTabBar,        "tb",        0,
  1117.     rtfParAttr,    rtfBorderTop,        "brdrt",    0,
  1118.     rtfParAttr,    rtfBorderBottom,    "brdrb",    0,
  1119.     rtfParAttr,    rtfBorderLeft,        "brdrl",    0,
  1120.     rtfParAttr,    rtfBorderRight,        "brdrr",    0,
  1121.     rtfParAttr,    rtfBorderBar,        "bar",        0,
  1122.     rtfParAttr,    rtfBorderBox,        "box",        0,
  1123.     rtfParAttr,    rtfBorderBetween,    "brdrbtw",    0,
  1124.     rtfParAttr,    rtfBorderSingle,    "brdrs",    0,
  1125.     rtfParAttr,    rtfBorderThick,        "brdrth",    0,
  1126.     rtfParAttr,    rtfBorderShadow,    "brdrsh",    0,
  1127.     rtfParAttr,    rtfBorderDouble,    "brdrdb",    0,
  1128.     rtfParAttr,    rtfBorderDot,        "brdrdot",    0,
  1129.     rtfParAttr,    rtfBorderHair,        "brdrhair",    0,
  1130.     rtfParAttr,    rtfBorderWidth,        "brdrw",    0,
  1131.     rtfParAttr,    rtfBorderColor,        "brdrcf",    0,
  1132.     rtfParAttr,    rtfBorderSpace,        "brsp",        0,
  1133.     rtfParAttr,    rtfLeaderDot,        "tldot",    0,
  1134.     rtfParAttr,    rtfLeaderHyphen,    "tlhyph",    0,
  1135.     rtfParAttr,    rtfLeaderUnder,        "tlul",        0,
  1136.     rtfParAttr,    rtfLeaderThick,        "tlth",        0,
  1137.  
  1138.     rtfSectAttr,    rtfSectDef,        "sectd",    0,
  1139.     /*rtfSectAttr,    rtfNoBreak,        "nobreak",    0,
  1140.     rtfSectAttr,    rtfColBreak,        "colbreak",    0,
  1141.     rtfSectAttr,    rtfPageBreak,        "pagebreak",    0,
  1142.     rtfSectAttr,    rtfEvenBreak,        "evenbreak",    0,
  1143.     rtfSectAttr,    rtfOddBreak,        "oddbreak",    0,*/
  1144.     rtfSectAttr,    rtfNoBreak,        "sbknone",    0,
  1145.     rtfSectAttr,    rtfColBreak,        "sbkcol",    0,
  1146.     rtfSectAttr,    rtfPageBreak,        "sbkpage",    0,
  1147.     rtfSectAttr,    rtfEvenBreak,        "sbkeven",    0,
  1148.     rtfSectAttr,    rtfOddBreak,        "sbkodd",    0,
  1149.     rtfSectAttr,    rtfPageCont,        "pgncont",    0,
  1150.     rtfSectAttr,    rtfPageStarts,        "pgnstarts",    0,
  1151.     rtfSectAttr,    rtfPageRestart,        "pgnrestart",    0,
  1152.     rtfSectAttr,    rtfPageDecimal,        "pgndec",    0,
  1153.     rtfSectAttr,    rtfPageURoman,        "pgnucrm",    0,
  1154.     rtfSectAttr,    rtfPageLRoman,        "pgnlcrm",    0,
  1155.     rtfSectAttr,    rtfPageULetter,        "pgnucltr",    0,
  1156.     rtfSectAttr,    rtfPageLLetter,        "pgnlcltr",    0,
  1157.     rtfSectAttr,    rtfPageNumLeft,        "pgnx",        0,
  1158.     rtfSectAttr,    rtfPageNumTop,        "pgny",        0,
  1159.     rtfSectAttr,    rtfHeaderY,        "headery",    0,
  1160.     rtfSectAttr,    rtfFooterY,        "footery",    0,
  1161.     rtfSectAttr,    rtfLineModulus,        "linemod",    0,
  1162.     rtfSectAttr,    rtfLineDist,        "linex",    0,
  1163.     rtfSectAttr,    rtfLineStarts,        "linestarts",    0,
  1164.     rtfSectAttr,    rtfLineRestart,        "linerestart",    0,
  1165.     rtfSectAttr,    rtfLineRestartPg,    "lineppage",    0,
  1166.     rtfSectAttr,    rtfLineCont,        "linecont",    0,
  1167.     rtfSectAttr,    rtfTopVAlign,        "vertalt",    0,
  1168.     rtfSectAttr,    rtfBottomVAlign,    "vertal",    0,
  1169.     rtfSectAttr,    rtfCenterVAlign,    "vertalc",    0,
  1170.     rtfSectAttr,    rtfJustVAlign,        "vertalj",    0,
  1171.     rtfSectAttr,    rtfColumns,        "cols",        0,
  1172.     rtfSectAttr,    rtfColumnSpace,        "colsx",    0,
  1173.     rtfSectAttr,    rtfColumnLine,        "linebetcol",    0,
  1174.     rtfSectAttr,    rtfENoteHere,        "endnhere",    0,
  1175.     rtfSectAttr,    rtfTitleSpecial,    "titlepg",    0,
  1176.     rtfSectAttr,    rtfPrtBinFirst,        "binfsxn",    0,
  1177.     rtfSectAttr,    rtfPrtBin,        "binsxn",    0,
  1178.  
  1179.     rtfDocAttr,    rtfPaperWidth,        "paperw",    0,
  1180.     rtfDocAttr,    rtfPaperHeight,        "paperh",    0,
  1181.     rtfDocAttr,    rtfLeftMargin,        "margl",    0,
  1182.     rtfDocAttr,    rtfRightMargin,        "margr",    0,
  1183.     rtfDocAttr,    rtfTopMargin,        "margt",    0,
  1184.     rtfDocAttr,    rtfBottomMargin,    "margb",    0,
  1185.     rtfDocAttr,    rtfFacingPage,        "facingp",    0,
  1186.     rtfDocAttr,    rtfGutterWid,        "gutter",    0,
  1187.     rtfDocAttr,    rtfDefTab,        "deftab",    0,
  1188.     rtfDocAttr,    rtfWidowCtrl,        "widowctrl",    0,
  1189.     rtfDocAttr,    rtfHyphHotZone,        "hyphhotz",    0,
  1190.     rtfDocAttr,    rtfFNoteEndSect,    "endnotes",    0,
  1191.     rtfDocAttr,    rtfFNoteEndDoc,        "enddoc",    0,
  1192.     rtfDocAttr,    rtfFNoteBottom,        "ftnbj",    0,
  1193.     rtfDocAttr,    rtfFNoteText,        "ftntj",    0,
  1194.     rtfDocAttr,    rtfFNoteStart,        "ftnstart",    0,
  1195.     rtfDocAttr,    rtfFNoteRestart,    "ftnrestart",    0,
  1196.     rtfDocAttr,    rtfPageStart,        "pgnstart",    0,
  1197.     rtfDocAttr,    rtfLineStart,        "linestart",    0,
  1198.     rtfDocAttr,    rtfLandscape,        "landscape",    0,
  1199.     rtfDocAttr,    rtfFracWidth,        "fracwidth",    0,
  1200.     rtfDocAttr,    rtfNextFile,        "nextfile",    0,
  1201.     rtfDocAttr,    rtfTemplate,        "template",    0,
  1202.     rtfDocAttr,    rtfMakeBackup,        "makeback",    0,
  1203.     rtfDocAttr,    rtfMakeBackup,        "makebackup",    0,
  1204.     rtfDocAttr,    rtfRTFDefault,        "defformat",    0,
  1205.     rtfDocAttr,    rtfRevisions,        "revisions",    0,
  1206.     rtfDocAttr,    rtfMirrorMargin,    "margmirror",    0,
  1207.     rtfDocAttr,    rtfRevDisplay,        "revprop",    0,
  1208.     rtfDocAttr,    rtfRevBar,        "revbar",    0,
  1209.     rtfDocAttr,    rtfDefLanguage,        "deflang",    0,
  1210.  
  1211.     rtfStyleAttr,    rtfBasedOn,        "sbasedon",    0,
  1212.     rtfStyleAttr,    rtfNext,        "snext",    0,
  1213.  
  1214.     rtfPictAttr,    rtfMacQD,        "macpict",    0,
  1215.     rtfPictAttr,    rtfWinMetafile,        "wmetafile",    0,
  1216.     rtfPictAttr,    rtfWinBitmap,        "wbitmap",    0,
  1217.     rtfPictAttr,    rtfPicWid,        "picw",        0,
  1218.     rtfPictAttr,    rtfPicHt,        "pich",        0,
  1219.     rtfPictAttr,    rtfPicGoalWid,        "picwgoal",    0,
  1220.     rtfPictAttr,    rtfPicGoalWid,        "picwGoal",    0,
  1221.     rtfPictAttr,    rtfPicGoalHt,        "pichgoal",    0,
  1222.     rtfPictAttr,    rtfPicGoalHt,        "pichGoal",    0,
  1223.     rtfPictAttr,    rtfPicScaleX,        "picscalex",    0,
  1224.     rtfPictAttr,    rtfPicScaleY,        "picscaley",    0,
  1225.     rtfPictAttr,    rtfPicScaled,        "picscaled",    0,
  1226.     rtfPictAttr,    rtfPicCropTop,        "piccropt",    0,
  1227.     rtfPictAttr,    rtfPicCropBottom,    "piccropb",    0,
  1228.     rtfPictAttr,    rtfPicCropLeft,        "piccropl",    0,
  1229.     rtfPictAttr,    rtfPicCropRight,    "piccropr",    0,
  1230.     rtfPictAttr,    rtfPixelBits,        "wbmbitspixel",    0,
  1231.     rtfPictAttr,    rtfBitmapPlanes,    "wbmplanes",    0,
  1232.     rtfPictAttr,    rtfBitmapWid,        "wbmwidthbytes", 0,
  1233.     rtfPictAttr,    rtfPicBinary,        "bin",        0,
  1234.  
  1235.     rtfNeXTGrAttr,    rtfNeXTGWidth,        "width",    0,
  1236.     rtfNeXTGrAttr,    rtfNeXTGHeight,        "height",    0,
  1237.  
  1238.     rtfDestination,    rtfPict,        "pict",        0,
  1239.     rtfDestination,    rtfNeXTGraphic,        "NeXTGraphic",    0,
  1240.     rtfDestination,    rtfFootnote,        "footnote",    0,
  1241.     rtfDestination,    rtfHeader,        "header",    0,
  1242.     rtfDestination,    rtfHeaderLeft,        "headerl",    0,
  1243.     rtfDestination,    rtfHeaderRight,        "headerr",    0,
  1244.     rtfDestination,    rtfHeaderFirst,        "headerf",    0,
  1245.     rtfDestination,    rtfFooter,        "footer",    0,
  1246.     rtfDestination,    rtfFooterLeft,        "footerl",    0,
  1247.     rtfDestination,    rtfFooterRight,        "footerr",    0,
  1248.     rtfDestination,    rtfFooterFirst,        "footerf",    0,
  1249.     rtfDestination,    rtfFNSep,        "ftnsep",    0,
  1250.     rtfDestination,    rtfFNContSep,        "ftnsepc",    0,
  1251.     rtfDestination,    rtfFNContNotice,    "ftncn",    0,
  1252.     rtfDestination,    rtfInfo,        "info",        0,
  1253.     rtfDestination,    rtfStyleSheet,        "stylesheet",    0,
  1254.     rtfDestination,    rtfFontTbl,        "fonttbl",    0,
  1255.     rtfDestination,    rtfColorTbl,        "colortbl",    0,
  1256.     rtfDestination,    rtfAnnotation,        "annotation",    0,
  1257.     rtfDestination,    rtfAnnotID,        "atnid",    0,
  1258.     rtfDestination,    rtfField,        "field",    0,
  1259.     rtfDestination,    rtfFieldInst,        "fldinst",    0,
  1260.     rtfDestination,    rtfFieldResult,        "fldrslt",    0,
  1261.     rtfDestination,    rtfIndex,        "xe",        0,
  1262.     rtfDestination,    rtfIndexBold,        "bxe",        0,
  1263.     rtfDestination,    rtfIndexItalic,        "ixe",        0,
  1264.     rtfDestination,    rtfIndexText,        "txe",        0,
  1265.     rtfDestination,    rtfIndexRange,        "rxe",        0,
  1266.     rtfDestination,    rtfTOC,            "tc",        0,
  1267.     rtfDestination,    rtfBookmarkStart,    "bkmkstart",    0,
  1268.     rtfDestination,    rtfBookmarkEnd,        "bkmkend",    0,
  1269.     rtfDestination,    rtfITitle,        "title",    0,
  1270.     rtfDestination,    rtfISubject,        "subject",    0,
  1271.     rtfDestination,    rtfIAuthor,        "author",    0,
  1272.     rtfDestination,    rtfIOperator,        "operator",    0,
  1273.     rtfDestination,    rtfIKeywords,        "keywords",    0,
  1274.     rtfDestination,    rtfIComment,        "comment",    0,
  1275.     rtfDestination,    rtfIVersion,        "version",    0,
  1276.     rtfDestination,    rtfIDoccomm,        "doccomm",    0,
  1277.     rtfDestination,    rtfIVerscomm,        "verscomm",    0,
  1278.     rtfDestination,    rtfKeyCode,        "keycode",    0,
  1279.  
  1280.     rtfTOCAttr,    rtfTOCType,        "tcf",        0,
  1281.     rtfTOCAttr,    rtfTOCLevel,        "tcl",        0,
  1282.  
  1283.     rtfFontFamily,    rtfFFNil,        "fnil",        0,
  1284.     rtfFontFamily,    rtfFFRoman,        "froman",    0,
  1285.     rtfFontFamily,    rtfFFSwiss,        "fswiss",    0,
  1286.     rtfFontFamily,    rtfFFModern,        "fmodern",    0,
  1287.     rtfFontFamily,    rtfFFScript,        "fscript",    0,
  1288.     rtfFontFamily,    rtfFFDecor,        "fdecor",    0,
  1289.     rtfFontFamily,    rtfFFTech,        "ftech",    0,
  1290.  
  1291.     rtfColorName,    rtfRed,            "red",        0,
  1292.     rtfColorName,    rtfGreen,        "green",    0,
  1293.     rtfColorName,    rtfBlue,        "blue",        0,
  1294.  
  1295.     rtfCharSet,    rtfMacCharSet,        "mac",        0,
  1296.     rtfCharSet,    rtfAnsiCharSet,        "ansi",        0,
  1297.     rtfCharSet,    rtfPcCharSet,        "pc",        0,
  1298.     rtfCharSet,    rtfPcaCharSet,        "pca",        0,
  1299.  
  1300.     rtfTblAttr,    rtfCellBordBottom,    "clbrdrb",    0,
  1301.     rtfTblAttr,    rtfCellBordTop,        "clbrdrt",    0,
  1302.     rtfTblAttr,    rtfCellBordLeft,    "clbrdrl",    0,
  1303.     rtfTblAttr,    rtfCellBordRight,    "clbrdrr",    0,
  1304.     rtfTblAttr,    rtfRowDef,        "trowd",    0,
  1305.     rtfTblAttr,    rtfRowLeft,        "trql",        0,
  1306.     rtfTblAttr,    rtfRowRight,        "trqr",        0,
  1307.     rtfTblAttr,    rtfRowCenter,        "trqc",        0,
  1308.     rtfTblAttr,    rtfRowGapH,        "trgaph",    0,
  1309.     rtfTblAttr,    rtfRowHt,        "trrh",        0,
  1310.     rtfTblAttr,    rtfRowLeftEdge,        "trleft",    0,
  1311.     rtfTblAttr,    rtfCellPos,        "cellx",    0,
  1312.     rtfTblAttr,    rtfMergeRngFirst,    "clmgf",    0,
  1313.     rtfTblAttr,    rtfMergePrevious,    "clmrg",    0,
  1314.     rtfTblAttr,    rtfCellShading,        "clshdng",    0,
  1315.  
  1316.     rtfFieldAttr,    rtfFieldDirty,        "flddirty",    0,
  1317.     rtfFieldAttr,    rtfFieldEdited,        "fldedit",    0,
  1318.     rtfFieldAttr,    rtfFieldLocked,        "fldlock",    0,
  1319.     rtfFieldAttr,    rtfFieldPrivate,    "fldpriv",    0,
  1320.  
  1321.     rtfPosAttr,    rtfPosX,        "posx",        0,
  1322.     rtfPosAttr,    rtfPosXCenter,        "posxc",    0,
  1323.     rtfPosAttr,    rtfPosXInside,        "posxi",    0,
  1324.     rtfPosAttr,    rtfPosXLeft,        "posxl",    0,
  1325.     rtfPosAttr,    rtfPosXOutSide,        "posxo",    0,
  1326.     rtfPosAttr,    rtfPosXRight,        "posxr",    0,
  1327.     rtfPosAttr,    rtfPosY,        "posy",        0,
  1328.     rtfPosAttr,    rtfPosYInline,        "posyil",    0,
  1329.     rtfPosAttr,    rtfPosYTop,        "posyt",    0,
  1330.     rtfPosAttr,    rtfPosYCenter,        "posyc",    0,
  1331.     rtfPosAttr,    rtfPosYBottom,        "posyb",    0,
  1332.     rtfPosAttr,    rtfAbsWid,        "absw",        0,
  1333.     rtfPosAttr,    rtfTextDist,        "dxfrtext",    0,
  1334.     rtfPosAttr,    rtfRPosMargV,        "pvmrg",    0,
  1335.     rtfPosAttr,    rtfRPosPageV,        "pvpg",        0,
  1336.     rtfPosAttr,    rtfRPosMargH,        "phmrg",    0,
  1337.     rtfPosAttr,    rtfRPosPageH,        "phpg",        0,
  1338.     rtfPosAttr,    rtfRPosColH,        "phcol",    0,
  1339.  
  1340.     rtfVersion,    -1,            "rtf",        0,
  1341.     rtfDefFont,    -1,            "deff",        0,
  1342.  
  1343.     0,        -1,            (char *) NULL,    0
  1344. };
  1345.  
  1346.  
  1347. /*
  1348.     Initialize lookup table hash values.  Only need to do this the
  1349.     first time it's called.
  1350. */
  1351.  
  1352. static void LookupInit ()
  1353. {
  1354. static int    inited = 0;
  1355. RTFKey    *rp;
  1356.  
  1357.     if (inited == 0)
  1358.     {
  1359.         for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
  1360.             rp->rtfKHash = Hash (rp->rtfKStr);
  1361.         ++inited;
  1362.     }
  1363. }
  1364.  
  1365.  
  1366. /*
  1367.     Determine major and minor number of control token.  If it's
  1368.     not found, the class turns into rtfUnknown.
  1369. */
  1370.  
  1371. static void Lookup (s)
  1372. char    *s;
  1373. {
  1374. RTFKey    *rp;
  1375. int    hash;
  1376.  
  1377.     ++s;            /* skip over the leading \ character */
  1378.     hash = Hash (s);
  1379.     for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
  1380.     {
  1381.         if (hash == rp->rtfKHash && strcmp (s, rp->rtfKStr) == 0)
  1382.         {
  1383.             rtfClass = rtfControl;
  1384.             rtfMajor = rp->rtfKMajor;
  1385.             rtfMinor = rp->rtfKMinor;
  1386.             return;
  1387.         }
  1388.     }
  1389.     rtfClass = rtfUnknown;
  1390. }
  1391.  
  1392.  
  1393. /*
  1394.     Compute hash value of symbol
  1395. */
  1396.  
  1397. static int Hash (s)
  1398. char    *s;
  1399. {
  1400. char    c;
  1401. int    val = 0;
  1402.  
  1403.     while ((c = *s++) != '\0')
  1404.         val += (int) c;
  1405.     return (val);
  1406. }
  1407.  
  1408.  
  1409. /*
  1410.     Print helpful error message and give up
  1411. */
  1412.  
  1413. # ifdef STDARG
  1414.  
  1415. /*
  1416.  * This version is for systems with stdarg
  1417.  */
  1418.  
  1419. static void Error (char *fmt, ...)
  1420. {
  1421.     va_list args;
  1422.     va_start (args,fmt);
  1423.     vfprintf (stderr, fmt, args);
  1424.     va_end (args);
  1425.     fprintf (stderr, "\nLast token read was \"%s\"\n", rtfTextBuf);
  1426.     exit (1);
  1427. }
  1428.  
  1429. # else /* !STDARG */
  1430. # ifdef    VARARGS
  1431.  
  1432. /*
  1433.     This version is for systems that have varargs.
  1434. */
  1435.  
  1436. static void Error (va_alist)
  1437. va_dcl
  1438. {
  1439.     va_list    args;
  1440.     char    *fmt;
  1441.  
  1442.     va_start (args);
  1443.     fmt = va_arg (args, char *);
  1444.     vfprintf (stderr, fmt, args);
  1445.     va_end (args);
  1446.     fprintf (stderr, "\nLast token read was \"%s\"\n", rtfTextBuf);
  1447.     exit (1);
  1448. }
  1449.  
  1450. # else    /* !VARARGS */
  1451.  
  1452. /*
  1453.     This version is for systems that don't have varargs.
  1454. */
  1455.  
  1456. static void Error (fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
  1457.     char    *fmt;
  1458.     char    *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8, *a9;
  1459. {
  1460.     fprintf (stderr, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  1461.     fprintf (stderr, "\nLast token read was \"%s\"\n", rtfTextBuf);
  1462.     exit (1);
  1463. }
  1464.  
  1465. # endif    /* !VARARGS */
  1466. # endif /* !STDARG */
  1467.  
  1468.  
  1469. /* ---------------------------------------------------------------------- */
  1470.  
  1471. /*
  1472.     Memory allocation routines
  1473. */
  1474.  
  1475.  
  1476. /*
  1477.     Return pointer to block of size bytes, or NULL if there's
  1478.     not enough memory available.
  1479. */
  1480.  
  1481. char *RTFAlloc (size)
  1482. int    size;
  1483. {
  1484.     return ((char *) malloc (size));
  1485. }
  1486.  
  1487.  
  1488. /*
  1489.     Saves a string on the heap and returns a pointer to it.
  1490. */
  1491.  
  1492.  
  1493. char *RTFStrSave (s)
  1494. char    *s;
  1495. {
  1496. char    *p;
  1497.  
  1498.     if ((p = RTFAlloc (strlen (s) + 1)) == (char *) NULL)
  1499.         return ((char *) NULL);
  1500.     return (strcpy (p, s));
  1501. }
  1502.  
  1503.  
  1504. void RTFFree (p)
  1505. char    *p;
  1506. {
  1507.     if (p != (char *) NULL)
  1508.         free (p);
  1509. }
  1510.  
  1511.  
  1512. /* ---------------------------------------------------------------------- */
  1513.  
  1514.  
  1515. /*
  1516.     Token comparison routines
  1517. */
  1518.  
  1519. int RTFCheckCM (class, major)
  1520. int    class, major;
  1521. {
  1522.     return (rtfClass == class && rtfMajor == major);
  1523. }
  1524.  
  1525.  
  1526. int RTFCheckCMM (class, major, minor)
  1527. int    class, major, minor;
  1528. {
  1529.     return (rtfClass == class && rtfMajor == major && rtfMinor == minor);
  1530. }
  1531.  
  1532.  
  1533. int RTFCheckMM (major, minor)
  1534. int    major, minor;
  1535. {
  1536.     return (rtfMajor == major && rtfMinor == minor);
  1537. }
  1538.